home *** CD-ROM | disk | FTP | other *** search
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Newsgroups: comp.lang.c
- Subject: Re: Passing Structures As
- Date: 3 Mar 1996 21:07:10 GMT
- Organization: Ripco Communications, Inc.
- Message-ID: <4hd1lu$1tp@gail.ripco.com>
- NNTP-Posting-Host: golden.ripco.com
-
- razine@aol.com (Razine) in <4hcov3$h1d@newsbf02.news.aol.com> asks:
-
- >I recently ran into a problem passing a structure as a pointer to a
- >function. I then wanted to pass a indivdual variable to another function
- >but it didnt work. Here is a brief example. Can anyone spot anything
- >wrong.
-
- As usual, my comments and changes to your code are marked with the
- comment tag `/* mha - ... */'. There are minor formatting changes also:
-
- #include <stdio.h> /* mha - added. Needed for printf(). */
-
- typedef struct {
- char name[81];
- int address_num;
- } person_rec;
-
- void display_person(person_rec * pr); /* mha - added prototype.
- * Otherwise, you will have
- * type mis-matches between the
- * implicit and explicit
- * declarations of
- * display_person */
-
- int /* mha - was `void' */ main(void)
- {
- person_rec thisuser = {"The Beast", 666}; /* mha - added
- * initialization so the
- * program actually
- * tests something */
- display_person(&thisuser);
- return 0; /* mha - added explicit return
- * [optional] */
- }
-
- void display_address(int address)
- {
- printf("Address Number is %d\r\n", address); /* mha - fixed `Adress'
- * [a nit] */
- /* mha - removed unnecessary `return;' [a nit] */
- }
-
- void display_person(person_rec * pr)
- {
-
- printf("Persons Name : %s\r\n", pr->name); /* another question why
- * in this instance
- * would I have to use
- * the -> operand? */
- /* mha - because pr is a pointer. I don't know what you want to
- * use instead. (*pr).name is possible, but that's just a synonym.
- * BTW, when you try to printf() an unintialized auto char[] as you
- * are doing here, you are lucky not to get a run-time error. I
- * have added an initialization above. */
-
- display_address(pr->address_num);
- /* Basically I get an error on the line above, how would I just
- * pass the interger value contained in pr->address_num */
-
- /* mha - What error do you get? pr->address_num is an int, and
- * display_address expects an int. If you get an diagnostic from
- * the compiler, I would suspect the compiler of having a problem.
- * If it is a run-time error, then the absence of a prototype,
- * added above, is the likely cause. */
-
- /* mha - removed unnecessary `return;' [a nit] */
- }
-
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-